package org.hgvc.webapp.ets.spring.dao;

import java.util.List;

import org.hibernate.Query;
import org.jocsb.base.AbstractHibernateDao;
import org.jocsb.base.bo.Role;
import org.jocsb.base.bo.User;
import org.jocsb.base.bo.UserRole;

public class AdminHbmDao extends AbstractHibernateDao {

	public UserRole getUserRoleByUserId(String id)throws Exception{
		StringBuffer str = new StringBuffer();
		str.append(" select userrole from UserRole userrole where userrole.user.userId=:id");
		
		Query query = getSession().createQuery(str.toString());
		query.setParameter("id", id);
		
		return (UserRole)query.uniqueResult();
	}
	
	public List<Role> getRoleList()throws Exception{
		StringBuffer str = new StringBuffer();
		str.append(" select role from Role role");
		
		Query query = getSession().createQuery(str.toString());
		return (List<Role>)query.list();
		
	}
	
	public User loadUserByUsername(String username)throws Exception{
		StringBuffer str = new StringBuffer();
		str.append("select usr from User usr where usr.username=:username");
		
		Query query = getSession().createQuery(str.toString());
		query.setParameter("username", username);
		
		return (User)query.uniqueResult();
	}
	
	public List<User> getUserByRole(String roleId)throws Exception{
		
		StringBuffer str = new StringBuffer();
		str.append("select role.user from UserRole role where role.role.roleId=:roleId");
		Query query = getSession().createQuery(str.toString());
		query.setParameter("roleId", roleId);
		return (List<User>)query.list();
	}
	
	public void deleteUserRole(String userId)throws Exception{
		StringBuffer str = new StringBuffer();
		str.append("delete from UserRole ur where ur.user.userId=:userId");
		Query query = getSession().createQuery(str.toString());
		query.setParameter("userId", userId);
		query.executeUpdate();
	}
	
}
